package com.fzshang.shopping.wx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzshang.shopping.commen.bean.Good;
import com.fzshang.shopping.commen.bean.GoodImage;
import com.fzshang.shopping.commen.bean.ShoppingOrder;
import com.fzshang.shopping.commen.bean.User;
import com.fzshang.shopping.commen.controller.BaseController;
import com.fzshang.shopping.commen.entity.ApiResult;
import com.fzshang.shopping.commen.util.WL.WL;
import com.fzshang.shopping.commen.util.data.Date;
import com.fzshang.shopping.commen.util.zhifubao.OrderIdUtil;
import com.google.gson.Gson;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-03-28
 */
@Controller
@RequestMapping("wx/shoppingOrder")
public class WXShoppingOrderController extends BaseController {

    @RequestMapping(value = "shoppingOrder.html")
    public String shoppingOrder(Model model,HttpServletRequest request){
        User user = getWxUser(request);
        List<ShoppingOrder> shoppingOrders = shoppingOrderService.selectOrderDesc(user.getOpenid());
        for (int i = 0; i < shoppingOrders.size(); i++) {
            ShoppingOrder shoppingOrder = shoppingOrders.get(i);
             QueryWrapper queryWrapper1 = new QueryWrapper();
             queryWrapper1.eq("id",shoppingOrder.getGoodId());
            Good good = goodService.getOne(queryWrapper1);
            shoppingOrder.setGoodName(good.getGoodName());
            //根据商品id查询图片
            QueryWrapper queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("good_id",good.getId());
            List<GoodImage> goodImages = goodImageService.list(queryWrapper2);
            shoppingOrder.setGoodUrl(goodImages.get(0).getUrl());
            shoppingOrder.setTime(Date.date(shoppingOrder.getCreateTime()));
        }
        if(shoppingOrders.size()>0){
            model.addAttribute("size",1);
            model.addAttribute("orderList",shoppingOrders);
        }else{
            model.addAttribute("size",0);
        }
        return "wx/order/order";
    }

    /**
     * @param model
     * @param request
     * @return 待付款
     */
    @RequestMapping(value = "daiOrder.html")
    public String daiOrder(Model model,HttpServletRequest request){
        User user = getWxUser(request);
        List<ShoppingOrder> shoppingOrders = shoppingOrderService.selectDaiFuKuanOrderDesc(user.getOpenid());
        for (int i = 0; i < shoppingOrders.size(); i++) {
            ShoppingOrder shoppingOrder = shoppingOrders.get(i);
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("id",shoppingOrder.getGoodId());
            Good good = goodService.getOne(queryWrapper1);
            shoppingOrder.setGoodName(good.getGoodName());
            //根据商品id查询图片
            QueryWrapper queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("good_id",good.getId());
            List<GoodImage> goodImages = goodImageService.list(queryWrapper2);
            shoppingOrder.setGoodUrl(goodImages.get(0).getUrl());
            shoppingOrder.setTime(Date.date(shoppingOrder.getCreateTime()));
        }
        if(shoppingOrders.size()>0){
            model.addAttribute("size",1);
            model.addAttribute("orderList",shoppingOrders);
        }else{
            model.addAttribute("size",0);
        }
        return "wx/order/daiOrder";
    }

    /**
     * @param model
     * @param request
     * @return 待发货
     */
    @RequestMapping(value = "daiFaHuo.html")
    public String daiFaHuo(Model model,HttpServletRequest request){
        User user = getWxUser(request);
        List<ShoppingOrder> shoppingOrders = shoppingOrderService.selectDaiFaHuoOrderDesc(user.getOpenid());
        for (int i = 0; i < shoppingOrders.size(); i++) {
            ShoppingOrder shoppingOrder = shoppingOrders.get(i);
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("id",shoppingOrder.getGoodId());
            Good good = goodService.getOne(queryWrapper1);
            shoppingOrder.setGoodName(good.getGoodName());
            //根据商品id查询图片
            QueryWrapper queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("good_id",good.getId());
            List<GoodImage> goodImages = goodImageService.list(queryWrapper2);
            shoppingOrder.setGoodUrl(goodImages.get(0).getUrl());
            shoppingOrder.setTime(Date.date(shoppingOrder.getCreateTime()));
        }
        if(shoppingOrders.size()>0){
            model.addAttribute("size",1);
            model.addAttribute("orderList",shoppingOrders);
        }else{
            model.addAttribute("size",0);
        }

        return "wx/order/daiFaHuo";
    }

    /**
     * @param model
     * @param request
     * @return 待收货
     */
    @RequestMapping(value = "daiShouHuo.html")
    public String daiShouHuo(Model model,HttpServletRequest request){
        User user = getWxUser(request);
        List<ShoppingOrder> shoppingOrders = shoppingOrderService.selectDaiShouHuoOrderDesc(user.getOpenid());
        for (int i = 0; i < shoppingOrders.size(); i++) {
            ShoppingOrder shoppingOrder = shoppingOrders.get(i);
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("id",shoppingOrder.getGoodId());
            Good good = goodService.getOne(queryWrapper1);
            shoppingOrder.setGoodName(good.getGoodName());
            //根据商品id查询图片
            QueryWrapper queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("good_id",good.getId());
            List<GoodImage> goodImages = goodImageService.list(queryWrapper2);
            shoppingOrder.setGoodUrl(goodImages.get(0).getUrl());
            shoppingOrder.setTime(Date.date(shoppingOrder.getCreateTime()));
        }
        if(shoppingOrders.size()>0){
            model.addAttribute("size",1);
            model.addAttribute("orderList",shoppingOrders);
        }else{
            model.addAttribute("size",0);
        }

        return "wx/order/daiShouHuo";
    }

    /**
     * @param model
     * @param request
     * @return 退货售后
     */
    @RequestMapping(value = "tuiShou.html")
    public String tuiShou(Model model,HttpServletRequest request){
        User user = getWxUser(request);
        List<ShoppingOrder> shoppingOrders = shoppingOrderService.selectTuiSHouOrderDesc(user.getOpenid());
        for (int i = 0; i < shoppingOrders.size(); i++) {
            ShoppingOrder shoppingOrder = shoppingOrders.get(i);
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("id",shoppingOrder.getGoodId());
            Good good = goodService.getOne(queryWrapper1);
            shoppingOrder.setGoodName(good.getGoodName());
            //根据商品id查询图片
            QueryWrapper queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("good_id",good.getId());
            List<GoodImage> goodImages = goodImageService.list(queryWrapper2);
            shoppingOrder.setGoodUrl(goodImages.get(0).getUrl());
            shoppingOrder.setTime(Date.date(shoppingOrder.getCreateTime()));
        }
        if(shoppingOrders.size()>0){
            model.addAttribute("size",1);
            model.addAttribute("orderList",shoppingOrders);
        }else{
            model.addAttribute("size",0);
        }

        return "wx/order/tuiShou";
    }



    /**
     * @param shoppingOrder
     * @return 添加信息
     */
    @RequestMapping(value = "saveOrder")
    @ResponseBody
    public ApiResult saveOrder(@RequestBody ShoppingOrder shoppingOrder, HttpServletRequest request){
        shoppingOrder.setCreateTime(System.currentTimeMillis());
        shoppingOrder.setState(2);
        shoppingOrder.setOddNumber(OrderIdUtil.gen());
        shoppingOrder.setType(2);
        shoppingOrder.setPhone(getWxUser(request).getTel());
        shoppingOrder.setOpenid(getWxUser(request).getOpenid());
        if(shoppingOrderService.save(shoppingOrder)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

    /**
     * @return 跳转物流查询
     */
    @RequestMapping(value = "wl.html")
    public String wl(Model model,String kd){
        model.addAttribute("kd",kd);
        return "wx/order/wl";
    }
    /**
     * @param oddNumber
     * @return 物流查询
     */
    @RequestMapping(value = "selectLogisticsExpress")
    @ResponseBody
    public ApiResult selectLogisticsExpress(String oddNumber){
        return WL.getLogisticsExpress(oddNumber);
    }

    /**
     * @param shoppingOrder
     * @return 签收
     */
    @RequestMapping(value = "qianShou")
    @ResponseBody
    public ApiResult qianShou(@RequestBody ShoppingOrder shoppingOrder){
        shoppingOrder.setState(7);
        shoppingOrder.setType(1);
        shoppingOrder.setSignTime(System.currentTimeMillis());
        if(shoppingOrderService.updateById(shoppingOrder)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

}
